package com.zcl.servlet;

import com.alibaba.fastjson.JSON;
import com.zcl.pojo.PageResult;
import com.zcl.pojo.R;
import com.zcl.pojo.student;
import com.zcl.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        studentService = wac.getBean(StudentService.class);
    }

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //设置响应字符集编码
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=utf8");
        //根据请求参数调用不同方法
        String cmd = req.getParameter("cmd");
        //判断参数是否为空
        if (StringUtils.isNoneBlank(cmd)){
            if ("list".equals(cmd)){
                list(req,res);               //查询所有学生
            }else if ("findByPage".equals(cmd)){
                findByPage(req,res);        //分页查询
            }else if ("search".equals(cmd)){
                search(req,res);            //条件查询+分页
            }else if ("add".equals(cmd)){
                add(req,res);               //添加学生
            }else if ("update".equals(cmd)){
                update(req,res);            //修改学生
            }else if ("delete".equals(cmd)){
                delete(req,res);            //删除学生
            }
        }


    }

    private void delete(HttpServletRequest req, HttpServletResponse res) throws IOException {
        //删除学生
        String sids = req.getParameter("sids");
        //定义要删除的学生的学号的集合
        List<String> ids = new ArrayList<>();
        //拆分学号的集合
         String[] split = sids.split(",");
        //加入到list集合
        if (split.length==0){
            ids.add(sids);
        }else {
            //转换为list集合
            ids=Arrays.asList(split);
        }
        studentService.delete(ids);
        //将学生列表放入R对象结果集
        R r = R.ok().put("message","删除学生成功");
        //输出结果
        res.getWriter().println(JSON.toJSONString(r));
        res.getWriter().flush();

    }

    private void update(HttpServletRequest req, HttpServletResponse res) throws IOException {
        //修改学生,得到传入的数据
         String update = req.getParameter("stud");
         //转化为学生对象
         student student = JSON.parseObject(update, student.class);
         //调用方法修改学生
        studentService.update(student);
        //        将学生列表放入R对象结果集
        R r = R.ok().put("message", "哒哒哒滴答!修改成功!");
//        将结果集对象转化成JSON字符串
        String s = JSON.toJSONString(r);
        //输出结果
        res.getWriter().println(s);
        res.getWriter().flush();

    }

    private void add(HttpServletRequest req, HttpServletResponse res) throws IOException {
        //添加学生,得到传入的参数
        String stud = req.getParameter("stud");
        //转换成为学生对象
         student student = JSON.parseObject(stud,student.class);
         //保存学生对象
        studentService.insert(student);
        //        将学生列表放入R对象结果集
        R r = R.ok().put("message","添加学生成功");
        //输出结果
        res.getWriter().println(JSON.toJSONString(r));
        res.getWriter().flush();

    }

    private void search(HttpServletRequest req, HttpServletResponse res) throws IOException {
        //条件查询
        //得到分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //参数转化成int类型
        int page = Integer.parseInt(pageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //得到分页查询对象
        String stud = req.getParameter("stud");
        System.out.println(stud);
//        转化成java对象
         student student = JSON.parseObject(stud,student.class);
         //得到分页结果集对象
        PageResult<student> pr=studentService.search(page,pageSize,student);
        //        将学生列表放入R对象结果集
        R r = R.ok().put("pr", pr);
//        将结果集对象转化成JSON字符串
        String s = JSON.toJSONString(r);
        //输出结果
        res.getWriter().println(s);
        res.getWriter().flush();
    }

    private void findByPage(HttpServletRequest req, HttpServletResponse res) throws IOException {
        //分页查询,得到请求参数
        int page = Integer.parseInt(req.getParameter("page"));
        int PageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageResult<student> pr= studentService.findByPage(page,PageSize);
        //        将学生列表放入R对象结果集
        R r = R.ok().put("pr", pr);
//        将结果集对象转化成JSON字符串
        String s = JSON.toJSONString(r);
        //输出结果
        res.getWriter().println(s);
        res.getWriter().flush();
    }

    //查询所有学生
    private void list(HttpServletRequest req, HttpServletResponse res) throws IOException {
//        通过findAll方法获取所有学生列表
        List<student> students =studentService.findAll();
//        将学生列表放入R对象结果集
         R r = R.ok().put("list", students);
//        将结果集对象转化成JSON字符串
         String s = JSON.toJSONString(r);
         //输出结果
        res.getWriter().println(s);
        res.getWriter().flush();

    }

}
